from django.shortcuts import render
from django.views import View
from django import http
from django.core.cache import cache
import logging
from nxh_mall.utils.response_code import RETCODE
from .models import Province

logger = logging.getLogger('django')
# Create your views here.
"""
查询省份：city=0
查询市：city！=0 and area=0
查询县/区域：city!=0 area !=0 and town=0
查询乡镇：
http://127.0.0.1:8001/areas/?province_id=64&city_id=01&area_id=04
"""


class AreasView(View):
    def get(self, request):
        province_id = request.GET.get('province_id')
        city_id = request.GET.get('city_id')
        area_id = request.GET.get('area_id')
        # town_id = request.GET.get('town_id')

        if area_id:  # 查询乡镇级
            # 查询乡镇
            try:
                town_model_list = Province.objects.filter(province=province_id, city=city_id, area=area_id).exclude(
                    town='0')

                town_list = []
                for town in town_model_list:
                    town_dict = {
                        'id': town.id,
                        'name': town.name,
                        'code': town.code,
                        'province': town.province,
                        'city': town.city,
                        'area': town.area,
                    }
                    town_list.append(town_dict)

                data = {'code': RETCODE.OK,
                        'errmsg': 'OK',
                        'sub_data': {
                            'province_id': province_id,
                            'city_id': city_id,
                            'area_id': area_id,
                            'list': town_list
                        }}
                return http.JsonResponse(data)
            except Exception as e:
                logger.error(e)
                data = {'code': RETCODE.DBERR,
                        'errmsg': '查询省份数据错误',
                        'sub_data': {}}
                return http.JsonResponse(data)
        elif city_id:  # 查询县级
            # 缓存数据
            cachekey = 'area_list' + province_id+city_id
            area_list = cache.get(cachekey)
            if not area_list:
                try:
                    area_model_list = Province.objects.filter(province=province_id, city=city_id, town='0').exclude(
                        area='0')

                    area_list = []
                    for area in area_model_list:
                        area_dict = {
                            'id': area.id,
                            'name': area.name,
                            'code': area.code,
                            'province': area.province,
                            'city': area.city,
                            'area': area.area,
                        }
                        area_list.append(area_dict)
                    #缓存数据
                    cache.set(cachekey, area_list, 3600)

                except Exception as e:
                    logger.error(e)
                    data = {'code': RETCODE.DBERR,
                            'errmsg': '查询省份数据错误',
                            }
                    return http.JsonResponse(data)
            data = {'code': RETCODE.OK,
                    'errmsg': 'OK',
                    'sub_data': {
                        'province_id': province_id,
                        'city_id': city_id,
                        'list': area_list
                    }}
            return http.JsonResponse(data)

        elif province_id:  # 查询城市
            # 缓存数据
            cachekey = 'city_list' + province_id
            city_list = cache.get(cachekey)
            if not city_list:
                try:
                    city_model_list = Province.objects.filter(province=province_id, area='0').exclude(city='0')
                    if len(city_model_list) == 0:  # 直辖市的情况
                        city_model_list = Province.objects.filter(province=province_id, town='0').exclude(city='0').exclude(
                            area='0')
                    city_list = []
                    for city in city_model_list:
                        city_dict = {
                            'id': city.id,
                            'name': city.name,
                            'code': city.code,
                            'province': city.province,
                            'city': city.city,
                            'area': city.area,
                        }
                        city_list.append(city_dict)
                    # 缓存数据
                    cache.set(cachekey, city_list, 3600)

                except Exception as e:
                    logger.error(e)
                    data = {'code': RETCODE.DBERR,
                            'errmsg': '查询省份数据错误',
                            'province_list': []}
                    return http.JsonResponse(data)
            data = {'code': RETCODE.OK,
                    'errmsg': 'OK',
                    'sub_data': {
                        'province_id': province_id,
                        'list': city_list
                    }}
            return http.JsonResponse(data)

        elif not province_id:  # 查询所有省份
            province_list = cache.get('province_list')
            if not province_list:
                try:
                    province_model_list = Province.objects.filter(city='0')
                    province_list = []
                    for province in province_model_list:
                        province_dict = {
                            'id': province.id,
                            'name': province.name,
                            'code': province.code,
                            'province': province.province,
                            'city': province.city,
                            'area': province.area,
                        }
                        province_list.append(province_dict)
                    # 缓存数据
                    cache.set('province_list', province_list, 3600)

                except Exception as e:
                    logger.error(e)
                    data = {'code': RETCODE.DBERR,
                            'errmsg': '查询省份数据错误',
                            'province_list': []}
                    return http.JsonResponse(data)

            data = {'code': RETCODE.OK,
                    'errmsg': 'OK',
                    'sub_data': {
                        'list': province_list
                    }}
            return http.JsonResponse(data)



        else:
            print('出错了？？？？？？')
